package com.Listeners;


import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TableRow;
import com.Managers.FragmentManagerClass;
import com.Managers.HomeManagerClass;
import com.Managers.LogManagerClass;
import com.SoapClasses.SoapUtil;
import com.activities.ActivityVerifyLogin;
import com.shareup.R;
import com.utils.PagerUtil;


public class MenuClickListener implements OnItemClickListener {

    public static View currentSelectedView;

    public final static int ITEM_TYPE_HEADER = 110;

    public static void highlightCurrentRow(View rowView) {

        rowView.setBackgroundResource(R.drawable.color_gradient_selection);
        TableRow refreshContainer = (TableRow) rowView.findViewById(R.id.table_refresh);
        if (refreshContainer != null) {
            refreshContainer.setVisibility(View.VISIBLE);
        }
    }

    private static boolean isNumeric(String cadena) {

        try {
            Integer.parseInt(cadena);
            return true;
        }
        catch (NumberFormatException x) {
            Log.e("MenuClickListener: ", x.getMessage());
            LogManagerClass.writeLog("MenuClickListener: "
                        + x.getMessage());
            return false;
        }
    }

    private Context context;

    private HomeManagerClass hmanager;

    public MenuClickListener(Context context,
                HomeManagerClass hmanager) {

        super();
        this.context = context;
        this.hmanager = hmanager;

    }

    @Override
    public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {

        int TAG = 0;
        if (v.getTag().equals(ITEM_TYPE_HEADER)) {

        }
        else
            if (currentSelectedView != null
                        && currentSelectedView != v) {
                unhighlightCurrentRow(currentSelectedView);
            }

        if (!isNumeric(v.getTag().toString())) {

            TAG = SoapUtil.MODE_TAGS;
            SoapUtil.MENU_VIEW = TAG;
            hmanager.hideTab();
            unhighlightCurrentRow(v);

            this.hmanager.changeViewMenu(PagerUtil.FRAGMENT_TAGS);
            FragmentManagerClass.aExplore.loadFromTags(v.getTag().toString());

        }
        else {
            // IF ITS NOT A HEADER OR A SEARCH BY TAG FILTER
            TAG = Integer.parseInt(v.getTag().toString());

            switch (TAG) {
                case SoapUtil.MODE_NEWS:
                    TAG = SoapUtil.MODE_NEWS;
                    if (SoapUtil.MENU_VIEW != SoapUtil.MODE_NEWS
                                && SoapUtil.MENU_VIEW != SoapUtil.MODE_MINE) {

                        this.hmanager.changeViewMenu(PagerUtil.FRAGMENT_NEWS);
                        hmanager.showTab();
                        unhighlightCurrentRow(v);
                        SoapUtil.MENU_VIEW = SoapUtil.MODE_NEWS;
                        FragmentManagerClass.aExplore.loadFrom(TAG, 0);

                    }
                    break;
                case SoapUtil.MODE_EXPLORE:
                    TAG = SoapUtil.MODE_EXPLORE;
                    if (SoapUtil.MENU_VIEW != SoapUtil.MODE_EXPLORE) {

                        this.hmanager.changeViewMenu(PagerUtil.FRAGMENT_EXPLORE);
                        this.hmanager.hideTab();
                        SoapUtil.MENU_VIEW = SoapUtil.MODE_EXPLORE;
                        unhighlightCurrentRow(v);
                        FragmentManagerClass.aExplore.loadFrom(TAG, 0);

                    }
                    break;
                case SoapUtil.MODE_FAVS:
                    TAG = SoapUtil.MODE_FAVS;
                    if (SoapUtil.MENU_VIEW != SoapUtil.MODE_FAVS) {
                        SoapUtil.MENU_VIEW = SoapUtil.MODE_FAVS;

                        this.hmanager.changeViewMenu(PagerUtil.FRAGMENT_FAVORITE);
                        this.hmanager.hideTab();
                        unhighlightCurrentRow(v);
                        FragmentManagerClass.aExplore.clear();
                        FragmentManagerClass.aExplore.loadFrom(TAG, 0);

                    }
                    break;
                case SoapUtil.MODE_FOLLOWERS:
                    TAG = SoapUtil.MODE_FOLLOWERS;
                    if (SoapUtil.MENU_VIEW != SoapUtil.MODE_FOLLOWERS) {

                        hmanager.hideTab();
                        hmanager.changeViewMenu(PagerUtil.FRAGMENT_FOLLOWERS);
                        SoapUtil.MENU_VIEW = SoapUtil.MODE_FOLLOWERS;
                        FragmentManagerClass.aFollow.loadFollows();

                        unhighlightCurrentRow(v);
                    }
                    break;
                case SoapUtil.MODE_LOGOUT:
                    hmanager.destroy();
                    Intent intent = new Intent(context, ActivityVerifyLogin.class);
                    intent.putExtra("logout", true);
                    context.startActivity(intent);
                    unhighlightCurrentRow(v);
                    break;
            }
        }

        if (!v.getTag().equals(ITEM_TYPE_HEADER)) {

            FragmentManagerClass.sMenu.toggle(true);
            currentSelectedView = v;
            highlightCurrentRow(currentSelectedView);
        }

    }

    private void unhighlightCurrentRow(View rowView) {

        rowView.setBackgroundResource(R.color.profile_rec);
        TableRow refreshContainer = (TableRow) rowView.findViewById(R.id.table_refresh);
        if (refreshContainer != null) {
            refreshContainer.setVisibility(View.GONE);
        }
    }

}
